package nl.hengelsportmolenberg.boilies.client.view;

//import java.text.DateFormat;
import java.util.ArrayList;
//import java.util.Date;

import nl.hengelsportmolenberg.boilies.client.presenter.CustomerCardPresenter;
import nl.hengelsportmolenberg.boilies.shared.CustomerCard;
import nl.hengelsportmolenberg.boilies.shared.OrderDetails;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class CustomerCardView extends Composite implements CustomerCardPresenter.Display {
	private final TextBox givenNameBox;
	private final TextBox familyNameBox;
	private final Button addOrderButton;
	private final TextBox orderAmountBox;
	private final FlexTable contentTable;
	private FlexTable ordersTable;
	private final TextArea customerComments;
	private final Button editCustomerButton;
	
	public CustomerCardView() {
	    DecoratorPanel contentTableDecorator = new DecoratorPanel();
	    initWidget(contentTableDecorator);
	    contentTableDecorator.setWidth("100%");
	    contentTableDecorator.setWidth("18em");
	    
	    contentTable = new FlexTable();
	    contentTable.setWidth("100%");
	    contentTable.getCellFormatter().addStyleName(0, 0, "orders-ListContainer");
	    contentTable.getCellFormatter().setWidth(0, 0, "100%");
	    contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);
	    
	    // Create the menu
	    //
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setBorderWidth(0);
	    hPanel.setSpacing(0);
	    hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
	    addOrderButton = new Button("Add");
	    hPanel.add(addOrderButton);
	    givenNameBox = new TextBox();
	    hPanel.add(givenNameBox);
	    familyNameBox = new TextBox();
	    hPanel.add(familyNameBox);
	    orderAmountBox = new TextBox();
	    hPanel.add(orderAmountBox);
	    customerComments = new TextArea();
	    hPanel.add(customerComments);
	    editCustomerButton = new Button("Edit Customer");
	    hPanel.add(editCustomerButton);
	    contentTable.getCellFormatter().addStyleName(0, 0, "orders-ListMenu");
	    contentTable.setWidget(0, 0, hPanel);
	    
	    // Create the order list
	    //
	    ordersTable = new FlexTable();
	    ordersTable.setCellSpacing(0);
	    ordersTable.setCellPadding(0);
	    ordersTable.setWidth("100%");
	    ordersTable.addStyleName("orders-ListContents");
	    ordersTable.getColumnFormatter().setWidth(0, "50px");
	    ordersTable.getColumnFormatter().setWidth(1, "50px");
	    contentTable.setWidget(1, 0, ordersTable);
	    
	    contentTableDecorator.add(contentTable);
	}
	
	public HasClickHandlers getAddOrderButton() {
		return addOrderButton;
	}
	
	public HasClickHandlers getEditCustomerButton() {
		return editCustomerButton;
	}
		  		  
	public HasClickHandlers getList() {
		return ordersTable;
	}
	 
	public void setData(CustomerCard card) {
		ArrayList<OrderDetails> orders = card.getOrders();
		for (int i = 0; i < orders.size(); ++i) {
			Integer amount = orders.get(i).getId();
			//Date date = orders.get(i).getDate();
		    ordersTable.setText(i, 0, amount.toString());
		    //ordersTable.setText(i, 1, DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(date));
		    ordersTable.setText(i, 1, "jan");
		}
	}
	 
	public int getClickedRow(ClickEvent event) {
		 int selectedRow = -1;
		 HTMLTable.Cell cell = ordersTable.getCellForEvent(event);
		 if (cell != null) {
			 selectedRow = cell.getRowIndex();
		 } 
		return selectedRow;
	 }
		
	public Widget asWidget() {
		return this;
	}
}
